#include "common/colours.h"
#include "common/settings.h"

#include <iostream>
#include <fstream>

#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QFile>
#include <common/xmlparser.h>

using namespace std;

void get_color(const QString &color, QVector<int> &values)
{
    QString path(workspace_directory());
    path.append("colours.xml");
    QFile colorFile(path);

    if (!colorFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        std::cout<<"new color file in:"<<path.toStdString()<<std::endl;
        createColorFile("colours");
        if (!colorFile.open(QIODevice::ReadOnly | QIODevice::Text))
            std::cout<<"cannot open in colours: color.txt"<<std::endl;
    }
    values = XmlParser::getInstance().searchColor(color);
    if(values.isEmpty()){
        //Default WHITE
        values[0] = 255;
        values[1] = 255;
        values[2] = 255;
    }

    //swap por que QMap cambia el orden de RGB;
    int aux = values[0];
    values[0] = values[2];
    values[2] = aux;

}

void createColorFile(const QString &colorFile)
{
    std::cout << "Color file created" << std::endl;

    QMap<QString, QMap<QString,float> > colors;

    QMap<QString,float> red;
    red.insert("R",255);
    red.insert("G",0);
    red.insert("B",0);
    colors.insert("RED",red);

    QMap<QString,float> white;
    white.insert("R",255);
    white.insert("G",255);
    white.insert("B",255);
    colors.insert("WHITE",white);

    QMap<QString,float> green;
    green.insert("R",0);
    green.insert("G",255);
    green.insert("B",0);
    colors.insert("GREEN",green);

    QMap<QString,float> blue;
    blue.insert("R",0);
    blue.insert("G",0);
    blue.insert("B",255);
    colors.insert("BLUE",blue);

    QMap<QString,float> yellow;
    yellow.insert("R",255);
    yellow.insert("G",255);
    yellow.insert("B",0);
    colors.insert("YELLOW",yellow);

    QMap<QString,float> black;
    black.insert("R",0);
    black.insert("G",0);
    black.insert("B",0);
    colors.insert("BLACK",black);

    QMap<QString,float> grey;
    grey.insert("R",152);
    grey.insert("G",152);
    grey.insert("B",152);
    colors.insert("GREY",grey);

    XmlParser::getInstance().writeXml(colorFile,QString(workspace_directory()), colors);


}
